package com.gs.SynAntiWandering.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.db.dto.MemberDto;
import com.gs.SynAntiWandering.db.dto.ShortDto;
import com.gs.SynAntiWandering.db.dto.TagDto;
import com.gs.SynAntiWandering.model.ComboModel;
import com.gs.SynAntiWandering.model.MemberModel;
import com.gs.SynAntiWandering.model.TagModel;
import com.gs.SynAntiWandering.model.UtilModel;
import com.gs.SynAntiWandering.model.listener.OnResultListener;
import com.gs.SynAntiWandering.view.IMemberEditView;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

public class MemberEditPresenter extends BasePresenter<IMemberEditView> {
    private MemberModel memberModel = new MemberModel();
    private UtilModel utilsModel = new UtilModel();
    private ComboModel comboModel = new ComboModel();
    private TagModel tagModel = new TagModel();

    public void member(String userId) throws IOException {
        if (getView() != null) {
            getView().showLoading();
            Type typeOf = new TypeToken<ApiResultData<MemberDto>>() {
            }.getType();
            memberModel.members(userId, new OnResultListener() {
                @Override
                public void onSuccess(Object apiResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetMemberSuccess((ApiResultData<MemberDto>) apiResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetMemberFails(e);
                    }
                }
            }, typeOf);
        }
    }

    public void add(MemberDto userDto) throws IOException {
        if (getView() != null) {
            getView().showLoading();
            memberModel.add(userDto, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onAddMemberSuccess((ApiResultData<String>) aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onAddMemberFails(e);
                    }
                }
            }, new TypeToken<ApiResultData<String>>(){}.getType());
        }
    }


    public void uploadFile(String path) throws IOException {
        if (getView() != null) {
            getView().showLoading();
            Type typeofT=new TypeToken<ApiResultData<String>>(){}.getType();
            utilsModel.upload(path, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onUploadFileSuccess((ApiResultData<String>)aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onUploadFileFails(e);
                    }
                }
            },typeofT);
        }
    }
    public void edit(MemberDto userDto) throws IOException {
        if (getView() != null) {
            getView().showLoading();
            memberModel.update(userDto, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onEditMemberSuccess((ApiResultData<String>) aipResult);
                    }
                }
                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onEditMemberFails(e);
                    }
                }

            },new TypeToken<ApiResultData<String>>(){}.getType());
        }
    }

    public void getComboList(String typeKey) throws IOException {
        if (getView()!= null) {
            getView().showLoading();
            Type typeofT=new TypeToken<ApiResultData<List<ShortDto>>>(){}.getType();
            comboModel.combo(typeKey, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetComListSuccess((ApiResultData<List<ShortDto>>)aipResult);
                    }
                }
                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetComListFails(e);
                    }
                }
            },typeofT);
        }
    }

    public void getTagList(String typeKey) throws IOException {
        if (getView()!= null) {
            getView().showLoading();
            Type typeofT=new TypeToken<ApiResultData<List<TagDto>>>(){}.getType();
            tagModel.getTag(typeKey, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetTagListSuccess((ApiResultData<List<TagDto>>)aipResult);
                    }
                }
                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetTagListFails(e);
                    }
                }
            },typeofT);
        }
    }
}


